package com.bwsoft.admin.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.NewsCategory;
import com.bwsoft.admin.domain.valueObject.NewsCategoryVO;


public class NewsCategoryAction extends BaseAction_ybd{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2932320957103337691L;
	private static Logger LOG = Logger.getLogger(NewsCategoryAction.class);
	
	private String status;

	public String categoryIndex(){
		return "categoryIndex";
	}

	/**
	 * 获取列表
	 */
	public String getNewsCategoryJson() {

		List<NewsCategory> NewsCategorySet=null;
		//资讯类别详情的分页集合
		NewsCategorySet = newsCategoryService.findData(page, rows);
		Long count = newsCategoryService.getTotalCount();
	
		List<NewsCategoryVO> NewsCategoryListVo = newsCategoryService.getVo(NewsCategorySet);
		responseJson.put("total", count);
		responseJson.put("rows", NewsCategoryListVo);

		return "mapJsonResult";
	}
	
	/**
	 * 新闻类别保存或新增
	 * @return
	 */
	public String saveOrUpdateNewsCategory(){
		
		newsCategory.setStatus(status==null ? false:status.equals("1"));

		msg = newsCategoryService.saveOrUpdateNewsCategory(newsCategory);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String removeNewsCategory(){
		msg = newsCategoryService.removeNewsCategory(newsCategory.getId());
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 获取资讯类别详情内容
	 * @return
	 */
	public String newsCategoryModifyInfo(){
		newsCategory=newsCategoryService.newsCategoryModifyInfo(newsCategory.getId());
		
		List<NewsCategory> newsCategoryList = new ArrayList<NewsCategory>();
		newsCategoryList.add(newsCategory);
		List<NewsCategoryVO> listVo = newsCategoryService.getVo(newsCategoryList);
		
		responseJson.put("newsCategory", listVo.get(0));
		return "mapJsonResult";
	}
	
	/**
	 * 添加资讯类别名称的验证
	 * @return
	 */
	public String newsCategoryInput(){
		
		msg = newsCategoryService.checkNewsCategoryInput(newsCategory);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
